package com.hkw.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

/**
 * 过滤器，对字符编码编码统一
 */
@WebFilter(filterName = "EncodingFilter",urlPatterns = "/*",initParams = {@WebInitParam(name="sname",value="EncodingFilter初始化完成")})
public class EncodingFilter implements Filter {
    public void destroy() {
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        chain.doFilter(req, resp);
    }
    public void init(FilterConfig config) throws ServletException {
        System.out.println(config.getInitParameter("sname"));
    }
}
